package xuperchain

import (
	"fmt"
	"time"

	"github.com/xuperchain/xuper-sdk-go/v2/account"
	"github.com/xuperchain/xuper-sdk-go/v2/xuper"
)

func AddViewRecord(papername string, username string) {

	account, err := account.GetAccountFromFile(path, pwd)
	if err != nil {
		fmt.Printf("GetAccountFromFile err: %v\n", err)
		return
	}
	fmt.Printf("privatekey Account address: %v\n", account.Address)

	err = account.SetContractAccount(contractAccount)
	if err != nil {
		panic(err)
	}

	xchainClient, err := xuper.New(nodeAddress)
	if err != nil {
		panic(err)
	}
	contractName := "xc_filestorage8"
	fmt.Println(papername)
	fmt.Println(username)
	currentTime := time.Now()
	// 格式化为小时:分钟:秒
	formattedTime := currentTime.Format("2006-01-02 15:04:05")
	fmt.Println(formattedTime)
	// 添加访问记录
	args := map[string]string{
		"_name":    papername,
		"userId":   username,
		"viewDate": formattedTime,
		"viewTime": formattedTime,
	}

	tx, err := xchainClient.InvokeEVMContract(account, contractName, "addViewRecord", args)
	if err != nil {
		panic(err)
	}
	fmt.Printf("addViewRecord SUCCESS! Response: %s\n", tx.ContractResponse.Body)

}
